<template>
  <div :id="echarts" :style="getheight"></div>
</template>
<script>
export default {
  name: 'curLine',
  mixins: [],
  computed: {
    echarts() {
      return 'echarts' + Math.random() * 100000
    },
    getheight() {
      return `height:${this.height}px`
    }
  },
  props: {
    chartData: {
      type: Object,
      default: () => []
    },
    height: {
      type: Number,
      default: 340
    },
    isSmooth: {
      type: Boolean,
      default: true
    },
    isArea: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  watch: {
    chartData() {
      this.draw()
    }
  },
  data() {
    return {
      list: [],
      option: {
        color: ['#45c946'],
        title: {
          text: this.title,
          textStyle: {
            fontSize: '18px',
            fontWeight: 'normal',
            color: '#fff'
          },
          top: '0',
          left: '30px'
        },
        grid: {
          top: '0',
          left: '0',
          right: '0',
          bottom: '0%',
          containLabel: true
        },
        legend: {
          orient: 'horizontal',
          show: false
        },
        tooltip: {
          trigger: 'axis',
          show: false
        },
        xAxis: {
          type: 'category',
          show: false,
          data: []
        },
        yAxis: {
          scale: true,
          show: false
        },
        series: []
      }
    }
  },
  mounted() {
    this.draw()
  },
  methods: {
    draw() {
      const chartFactor = this.$echarts.init(
        document.getElementById(this.echarts)
      )
      window.onresize = chartFactor.resize
      let List = this.chartData.y
      let series = []
      let xData = this.chartData.x
      for (let i in List) {
        let serie = {
          type: 'line',
          areaStyle: {},
          data: List[i],
          smooth: this.isSmooth
        }
        series.push(serie)
      }
      this.option.xAxis.data = xData[0]
      this.option.series = series
      chartFactor.setOption(this.option, true)
    }
  }
}
</script>
